package com.ak47.cms.cms.service

import com.ak47.cms.cms.builder.CrawlerWebClient
import com.ak47.cms.cms.dao.ImageRepository
import com.ak47.cms.cms.entity.Image
import org.jsoup.Jsoup
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.net.URL
import java.text.SimpleDateFormat
import java.util.*

@Service
class CrawMeiziImageService {
    private val logger = LoggerFactory.getLogger(CrawMeiziImageService::class.java)

    @Autowired
    private lateinit var imageRepository: ImageRepository

    private fun url(i: Int) = "https://www.mzitu.com/zipai/comment-page-${i}/"

    fun doCraw() {
        val urls = mutableSetOf<String>()

        for (i in 1..447) {
            urls.add(url(i))
        }

        urls.forEach {
            save(it)
        }
    }


    private fun save(beautyUrl: String) {
        val crawlerWebClient = CrawlerWebClient()
        val beautyPageHtml = crawlerWebClient.getPage(beautyUrl).asXml()
        val document = Jsoup.parse(beautyPageHtml)

        document.getElementById("comments")
                .child(0)
                .children()
                .forEach {

                    var url = it.childNode(1).childNode(5).childNode(1).attr("data-original")
                    logger.info("url = ${url}")

                    if (imageRepository.countByUrl(url) == 0) {
                        val image = Image()
                        image.category = "妹子图 ${SimpleDateFormat("yyyy-MM-dd").format(Date())}"
                        image.url = url
                        image.sourceType = 2
                        image.imageBlob = getByteArray(url)
                        imageRepository.save(image)
                    }
                }
    }


    private fun getByteArray(url: String): ByteArray {
        val urlObj = URL(url)
        return urlObj.readBytes()
    }
}
